// import { ElementRef, Injectable } from "@angular/core";
// import { PerspectiveCamera, Raycaster, Scene, Vector2, WebGLRenderer, WebGLRendererParameters } from "three";
// import { CameraService } from "./camera.service";
// import { SceneService } from "./scene.service";
// import { degToRad } from 'three/src/math/MathUtils.js';
// import { MessageService } from "./message.service";

// @Injectable()
// export class RendererService {
//     private camera = new PerspectiveCamera;
//     private scene = new Scene;
//     private renderer = new WebGLRenderer;
//     private lon = 0;
//     private lat = 0;
//     private isUserInteracting = false;
//     private onPointerDownPointerX = 0;
//     private onPointerDownPointerY = 0;
//     private onPointerDownLon = 0;
//     private onPointerDownLat = 0;
//     private camera_time = 0;
//     private mouse = new Vector2();
//     private raycaster = new Raycaster();
//     private sceneText = '';
//     private elementRef!: ElementRef ;

//     constructor(
//         private cameraService: CameraService,
//         private sceneService: SceneService,
//         private messageService: MessageService
//     ) {
//         this.messageService.On('mousedown', this.onMouseDown.bind(this))
//         this.messageService.On('mousemove', this.onMouseMove.bind(this))
//         this.messageService.On('mouseup', this.onMouseUp.bind(this))
//         this.messageService.On('mousewheel', this.onMouseWheel.bind(this))
//         this.messageService.On('windowresize', this.onWindowResize.bind(this))
//     }

//     webGL(parameters?: WebGLRendererParameters) {
//         return new WebGLRenderer(parameters)
//     }

//     init(elementRef: ElementRef) {
//         this.elementRef = elementRef

//         this.camera = this.cameraService.perspectiveCamera();
//         this.scene = this.sceneService.outsideScene();

//         this.renderer = this.webGL();
//         this.renderer.setPixelRatio(1);
//         this.renderer.setSize(this.elementRef.nativeElement.offsetWidth, this.elementRef.nativeElement.offsetHeight);

//         return this.renderer
//     }

//     animate() {
//         requestAnimationFrame(this.animate.bind(this));
//         this.messageService.Send('renderer_animate')
//         this.updateByMouse();
//     }

//     onMouseDown(event: MouseEvent) {
//         this.raycaster.setFromCamera(this.mouse, this.camera);


//         // 计算和所有模型的交集
//         const intersects = this.raycaster.intersectObjects(this.scene.children);
//         // 如果有交集，获取最近的一个
//         if (intersects.length > 0) {
//             // 假设我们要获取的数值是模型的name属性
//             const intersection = intersects[0];

//             // 这里可以添加更多的逻辑，比如根据模型的name做不同的处理
//             if (intersection.object.name === 'point_1_outside_car') {
//                 console.warn('不在车里')
//             } else if (intersection.object.name === 'point_2_outside_people') {
//                 console.warn('不在人群中')
//             } else if (intersection.object.name === 'point_0_outside_house') {
//                 console.warn('屋里可以检查下')
//                 this.sceneText = 'point_0_outside_house';
//             } else if (intersection.object.name === 'point_3_inside_eating_room') {
//                 console.warn('不在厨房')
//             } else if (intersection.object.name === 'point_4_inside_bed_room') {
//                 console.warn('小偷在这里')
//                 this.sceneService.addMurderer()
//             } else if (intersection.object.name === 'point_5_inside_back') {
//                 console.warn('返回广场')
//                 this.sceneText = 'point_5_inside_back';
//             }
//         }
//         this.isUserInteracting = true;
//         this.onPointerDownPointerX = event.clientX;
//         this.onPointerDownPointerY = event.clientY;
//         this.onPointerDownLon = this.lon;
//         this.onPointerDownLat = this.lat;
//     }
//     onMouseMove(event: MouseEvent) {
//         this.mouse.x = (event.clientX / this.elementRef.nativeElement.offsetWidth) * 2 - 1;
//         this.mouse.y = -(event.clientY / this.elementRef.nativeElement.offsetHeight) * 2 + 1;
//         if (this.isUserInteracting === true) {
//             this.lon = (this.onPointerDownPointerX - event.clientX) * 0.1 + this.onPointerDownLon;
//             this.lat = (event.clientY - this.onPointerDownPointerY) * 0.1 + this.onPointerDownLat;
//         }
//     }
//     onMouseUp(event: MouseEvent) {
//         this.isUserInteracting = false;
//     }
//     onMouseWheel(event: WheelEvent) {
//         this.camera.fov += event.deltaY * 0.05;
//         if (this.camera.fov < 20) {
//             this.camera.fov = 20
//         } else if (this.camera.fov > 100) {
//             this.camera.fov = 100
//         }
//         this.camera.updateProjectionMatrix();
//     }

//     onWindowResize() {
//         this.camera.aspect = this.elementRef.nativeElement.offsetWidth / this.elementRef.nativeElement.offsetHeight;
//         this.camera.updateProjectionMatrix();
//         this.renderer.setSize(this.elementRef.nativeElement.offsetWidth, this.elementRef.nativeElement.offsetHeight);
//     }

//     //捕捉鼠标
//     updateByMouse() {
//         this.lat = Math.max(- 85, Math.min(85, this.lat));
//         const phi = degToRad(90 - this.lat);
//         const theta = degToRad(this.lon);

//         let x = 500 * Math.sin(phi) * Math.cos(theta);
//         let y = 500 * Math.cos(phi);
//         let z = 500 * Math.sin(phi) * Math.sin(theta);
//         this.camera.lookAt(x, y, z);

//         if (this.sceneText) {
//             this.camera.fov -= 1;
//             this.camera_time++;
//             if (this.camera_time > 50) {
//                 this.camera_time = 0;
//                 this.camera.fov = 75;
//                 this.switchScene(this.sceneText);
//                 this.sceneText = '';
//             }
//             this.camera.updateProjectionMatrix();
//         }

//         this.renderer.render(this.scene, this.camera);

//     }

//     switchScene(text: string) {
//         if (text === 'point_0_outside_house') {
//             this.scene = this.sceneService.insideScene();
//         } else {
//             this.scene = this.sceneService.outsideScene();
//         }
//     }
// }